/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rvalerio.foregroundappchecker.slice;

import com.rvalerio.foregroundappchecker.ResUtil;
import com.rvalerio.foregroundappchecker.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.bundle.ElementName;
import ohos.rpc.IRemoteObject;

import static com.rvalerio.foregroundappchecker.Constants.SETTING_BUNDLE_NAME;
import static com.rvalerio.foregroundappchecker.Constants.SETTING_ABILITY_NAME;
import static com.rvalerio.foregroundappchecker.Constants.BUNDLE_NAME;
import static com.rvalerio.foregroundappchecker.Constants.ABILITY_NAME;

public class MainAbilitySlice extends AbilitySlice {

    Button mReqPermission,mStartService;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(this).
                parse(ResourceTable.Layout_ability_main, null, false);
        mReqPermission = (Button) rootLayout.findComponentById(ResourceTable.Id_req_permission);
        mStartService = (Button) rootLayout.findComponentById(ResourceTable.Id_startservice);

        mReqPermission.setClickedListener((Component c) -> {
            Intent i = new Intent();
            i.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName(SETTING_BUNDLE_NAME)
                    .withAbilityName(SETTING_ABILITY_NAME)
                    .build();
            i.setOperation(operation);
            startAbility(i);
        });

        mStartService.setClickedListener((Component c) -> {
            connectAbility(getIntent(),new AbilityConnection());
        });

        setShapeElement();
        super.setUIContent(rootLayout);
    }

    private Intent getIntent() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(BUNDLE_NAME)
                .withAbilityName(ABILITY_NAME)
                .build();
        intent.setOperation(operation);
        return intent;
    }

    class AbilityConnection implements IAbilityConnection {

        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
        }

        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int i) {
        }
    }

    private void setShapeElement() {
        mReqPermission.setBackground(ResUtil.getShapeElement
                (this, ShapeElement.RECTANGLE, ResourceTable.Color_material_light_gray, 5.0f));
        mStartService.setBackground(ResUtil.getShapeElement
                (this, ShapeElement.RECTANGLE, ResourceTable.Color_material_light_gray, 5.0f));
    }
}
